package com.business.frame.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.business.base.BaseController;
import com.business.base.Pager;
import com.business.frame.po.TypeBean;
import com.business.frame.service.FieldService;
import com.business.frame.service.TypeService;
import com.common.ErrorLog;
import com.common.RenderPage;
import com.common.StringProcess;
import com.common.utils.SpringContextHolder;

@Controller
@RequestMapping(value = "/frame/type")
public class TypeController {

	@Autowired
	private TypeService typeService;

	@RequestMapping({ "/list" })
	public String list(HttpServletRequest request, ModelMap model) {

		return "frame/type/list";
	}

	@RequestMapping({ "/detail" })
	public String detail(HttpServletRequest request, ModelMap model) {

		try {
			String typeId = StringProcess.getRequest(request.getParameter("typeId"), "");

			// 初始化控件

			// 类型下拉
			List<Map<String, Object>> typeList = new ArrayList<Map<String, Object>>();
			Map<String, Object> typeItem = new HashMap<String, Object>();
			typeItem.put("code", "number");
			typeItem.put("text", "数字");
			typeList.add(typeItem);
			typeItem = new HashMap<String, Object>();
			typeItem.put("code", "decimal");
			typeItem.put("text", "小数");
			typeList.add(typeItem);
			typeItem = new HashMap<String, Object>();
			typeItem.put("code", "string");
			typeItem.put("text", "字符");
			typeList.add(typeItem);
			typeItem = new HashMap<String, Object>();
			typeItem.put("code", "text");
			typeItem.put("text", "内容");
			typeList.add(typeItem);
			typeItem = new HashMap<String, Object>();
			typeItem.put("code", "datetime");
			typeItem.put("text", "日期");
			typeList.add(typeItem);
			typeItem = new HashMap<String, Object>();
			typeItem.put("code", "file");
			typeItem.put("text", "文件");
			typeList.add(typeItem);

			String action = "";
			TypeBean bean = new TypeBean();
			if (typeId.length() == 0) {
				action = "insert";
				typeId = StringProcess.getUUID();
				bean.setTypeId(typeId);
				bean.setFieldLength(0);
				bean.setOrderNumber(0);
			} else {
				action = "update";
				bean = typeService.getBeanByKeyId(typeId);
			}

			model.put("typeList", typeList);
			model.put("action", action);
			model.put("bean", bean);

		} catch (Exception e) {
			ErrorLog.OnWrite(e);
		}

		return "frame/type/detail";
	}

	@RequestMapping("/query")
	public void query(Integer pageIndex, Integer pageSize, HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {

		try {
			Map<String, Object> search = new HashMap<String, Object>();

			// 页面查询
			/*
			 * String fieldName = request.getParameter("txtFieldName");
			 * if(fieldName!=null) { fieldName = fieldName.trim(); if
			 * (fieldName.length() > 0) { search.put("fieldname", fieldName); }
			 * }
			 */
			// 页面查询

			Pager<Map<String, Object>> result = typeService.selectPage(pageIndex, pageSize, search);

			int resultCount = result.getList().size();
			for (int i = 0; i < result.getList().size(); i++) {
				Map<String, Object> item = result.getList().get(i);

				String isKey = StringProcess.convertToString(item.get("IsKey"));
				if (isKey.equals("1")) {
					isKey = "√";
				} else {
					isKey = "";
				}
				item.put("IsKey", isKey);

				String isAllowNull = StringProcess.convertToString(item.get("IsAllowNull"));
				if (isAllowNull.equals("1")) {
					isAllowNull = "√";
				} else {
					isAllowNull = "";
				}
				item.put("IsAllowNull", isAllowNull);
				
				item = BaseController.AddChangeRow(SpringContextHolder.getBean(TypeService.class), result.getList(), item, result.getTotal(), pageIndex, i, result.getPages(), pageSize, resultCount,"TypeId","OrderNumber", search);

			}

			RenderPage.JsonByObject(response, result);

		} catch (Exception e) {
			ErrorLog.OnWrite(e);
		}
	}

	@RequestMapping(params = "method=delete", method = RequestMethod.POST)
	public void del(HttpServletRequest request, HttpServletResponse response, String selectitem) {
		// TODO Auto-generated method stub

		try {
			String[] fieldIdArray = selectitem.split(",");

			for (String fieldId : fieldIdArray) {
				if (fieldId.length() > 0) {
					typeService.delete(fieldId);
				}
			}

			RenderPage.JsonByString(response, "1");
		} catch (Exception e) {
			ErrorLog.OnWrite(e);
		}
	}

	@RequestMapping(params = "method=save", method = RequestMethod.POST)
	public void save(HttpServletRequest request, HttpServletResponse response, String action) {

		try {
			String typeId = request.getParameter("txtTypeId");
			String fieldType = request.getParameter("lstFieldType");
			String fieldLength = request.getParameter("txtFieldLength");
			String isKey = request.getParameter("chkIsKey");
			String isAllowNull = request.getParameter("chkIsAllowNull");
			String isEnable = request.getParameter("lstIsEnable");
			String typeMsAccess = request.getParameter("txtTypeMsAccess");
			String typeMsSQL = request.getParameter("txtTypeMsSQL");
			String typeMySQL = request.getParameter("txtTypeMySQL");
			String typeOracle = request.getParameter("txtTypeOracle");
			String orderNumber = request.getParameter("txtOrderNumber");

			Map<String, Object> model = new HashMap<String, Object>();
			model.put("typeId", typeId);
			model.put("fieldType", fieldType);
			model.put("fieldLength", fieldLength);
			model.put("isKey", isKey);
			model.put("isAllowNull", isAllowNull);
			model.put("isEnable", isEnable);
			model.put("typeMsAccess", typeMsAccess);
			model.put("typeMsSQL", typeMsSQL);
			model.put("typeMySQL", typeMySQL);
			model.put("typeOracle", typeOracle);
			model.put("orderNumber", orderNumber);

			if (action.equals("insert")) {
				typeService.insert(model);
			} else if (action.equals("update")) {
				typeService.update(model);
			}

			RenderPage.JsonByString(response, "1");

		} catch (Exception e) {
			ErrorLog.OnWrite(e);
		}

	}

}
